package com.ufo.store.management.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ufo.store.management.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by taoli on 2016/10/31 0031.
 */

public class SearchHistoryRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context = null;
    private List<String> mList = new ArrayList<>();
    private OnClickListener mListener = null;
    
    public static class ViewHolder {
        public TextView nameTv;
        public TextView modeTv;
    }

    public SearchHistoryRecyclerViewAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<String> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    
    public String getItem(int position) {
        String item = null;
        if (position >= 0 && getItemCount() > position) {
            item = mList.get(position);
        }
        return item;
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder {
        TextView nameTv;
        public RecyclerViewHolder(View view) {
            super(view);
            nameTv = (TextView) view.findViewById(R.id.name_tv);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == 1) {
            holder = new RecyclerViewHolder(LayoutInflater.from(
                    this.context).inflate(R.layout.item_stock_search, parent,
                    false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final String info = mList.get(position);
        if (info != null) {
            if (holder instanceof RecyclerViewHolder) {
                RecyclerViewHolder holder1 = (RecyclerViewHolder) holder;
                holder1.nameTv.setText(info);
                holder1.nameTv.setTextColor(context.getResources().getColorStateList(R.color.text_stock_type_color));
                holder1.nameTv.setBackgroundResource(R.drawable.ic_stocktype_bg_selector);
            }
        }
        //点击事件
        if (mListener != null) {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建两个监听事件对象，增加了内存的开销
             */
            if (!holder.itemView.hasOnClickListeners()) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getPosition();
                        mListener.onItemClick(v, pos);
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = holder.getPosition();
                        mListener.onItemLongClick(v, pos);
                        return true;
                    }
                });
            }
        }
    }
    
    public void setOnClickListener(OnClickListener l) {
        mListener = l;
    }

    public interface OnClickListener {
        void onItemClick(View v, int position);

        void onItemLongClick(View v, int position);
    }
}
